package com.xxxy.dongyexuan.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("执行到拦截器了1"+request.getRequestURI());
//        if(request.getMethod().equals("OPTIONS")){
//            return true;
//        }
//        if ("OPTIONS".equals(request.getMethod())) {
//            String requestHeader = request.getHeader("Access-Control-Request-Headers");
//            if(requestHeader !=null) {
//                response.addHeader("Access-Control-Allow-Headers", requestHeader );
//            }
//            response.addHeader("Access-Control-Allow-Origin", "*");
//            return true;
//        }
//        if ("/getbiaozhi".equals(request.getRequestURI()) || "/dologin".equals(request.getRequestURI()) || "/image".equals(request.getRequestURI())) {
//            return true;
//        }
//        System.out.println("执行到拦截器了2");
//        String token = request.getHeader("Authorization");
//        if(token!=null){
//            System.out.println("通过的"+request.getMethod());
//            return true;
//        }else{
//            response.setStatus(403);
//            System.out.println("拦截器"+response);
//            return false;
//        }
//    }
//@Override
//public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//    String token = request.getHeader("Authorization");
//    if(request.getMethod().equals("OPTIONS")){
//        return true;
//    }
//    System.out.println("执行到拦截器...");
//    if (handler instanceof HandlerMethod) {
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        // 获取RequestMapping注解
//        RequestMapping requestMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);
//        String requestURI = request.getRequestURI();
//        if (requestMapping != null) {
//            // 获取value()属性中的路径数组
//            String[] paths = requestMapping.value();
//            // 遍历路径数组，检查是否有匹配的路径
//            for (String path : paths) {
//                if ("/getbiaozhi".equals(path) || requestURI.startsWith("/image/")) {
//                    // 如果是，则直接放行
//                    return true;
//                }
//            }
//        }
//    }
//    if (token!=null){
//        return true;
//    }else {
//        response.setStatus(403);
//        return false;
//    }
//}
}
